package com.chouren.goandroid.http.parser;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import org.apache.http.util.CharArrayBuffer;

/**
 * 文本数据的解析器
 * @purpose
 * @description
 * @version 1.0 
 * @author Administrator
 * @date 2014年7月1日
 * @update 2014年7月1日
 */
public class StringParser extends DataParser<String> {

	@Override
	public String parseData(InputStream is, int len, String charSet) throws IOException {
		return streamToString(is, len, charSet);
	}
	
	private String streamToString(InputStream is, int len, String charSet) throws IOException {
		Reader reader = new InputStreamReader(is);
		CharArrayBuffer buffer = new CharArrayBuffer(len);
		char[] temp = new char[buffSize];
		try {
			int length = 0;
			while(!Thread.currentThread().isInterrupted() && -1 != (length = reader.read(temp))) {
				buffer.append(temp, 0, length);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			reader.close();
		}
		
		return buffer.toString();
	}

}
